using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GriffinNav.Map.VirtualEarth;

namespace GriffinNav.Map
{
    public class Map: IMap
    {
        public event GenericEventHandler MapViewUpdated;
        public event MapErrorHandler OnMapError;

        private IMap mMap = null;

        public Map(string filePath, Rectangle screenRectangle)
        {
            // open map file
            mMap = new VirtualEarthMap(filePath, screenRectangle);

            // subscribe to events
            mMap.MapViewUpdated += new GenericEventHandler(mMap_MapViewUpdated);
            mMap.OnMapError += new MapErrorHandler(mMap_OnMapError);
        }

        private void mMap_OnMapError(MapException ex, bool showMessage)
        {
            if (OnMapError != null)
            {
                OnMapError(ex, showMessage);
            }
        }

        private void mMap_MapViewUpdated()
        {
            if (MapViewUpdated != null)
            {
                // raise map view updated event
                MapViewUpdated();
            }
        }

        public MapInfo MapInfo
        {
            get
            {
                return mMap.MapInfo;
            }
        }

        public Rectangle DisplayArea
        {
            get
            {
                return mMap.DisplayArea;
            }
        }

        public Rectangle BufferRectangle
        {
            get
            {
                return mMap.BufferRectangle;
            }
        }

        public Rectangle ScreenRectangle
        {
            set
            {
                mMap.ScreenRectangle = value;
            }
        }

        public Bitmap ScreenBuffer
        {
            get
            {
                return mMap.ScreenBuffer;
            }
        }

        public Graphics ScreenGraphics
        {
            get
            {
                return mMap.ScreenGraphics;
            }
        }

        public int PixelX
        {
            get
            {
                return mMap.PixelX;
            }
        }

        public int PixelY
        {
            get
            {
                return mMap.PixelY;
            }
        }

        public double Longitude
        {
            get
            {
                return mMap.Longitude;
            }
        }

        public double Latitude
        {
            get
            {
                return mMap.Latitude;
            }
        }

        public bool UpdatePending
        {
            get
            {
                return mMap.UpdatePending;
            }
        }

        public void Open(string filePath)
        {
            // open map file
            mMap.Open(filePath);
        }

        public void Open(string filePath, Size screenSize)
        {
            // open map file and change screen dimensions
            mMap.Open(filePath, screenSize);
        }

        public void Close()
        {
            // close map file
            mMap.Close();
        }

        public void DrawScreen()
        {
            // draw map view on the screen
            mMap.DrawScreen();
        }

        public void DrawScreen(Bitmap arrowImage, Rectangle arrowRectangle)
        {
            // draw map view on the screen
            mMap.DrawScreen(arrowImage, arrowRectangle);
        }

        public bool PositionOutOfBounds(double longitude, double latitude)
        {
            // check if position is outside of the current detail level bounds
            return mMap.PositionOutOfBounds(longitude, latitude);
        }

        public bool GoToPosition(int pixelX, int pixelY)
        {
            if (mMap.GoToPosition(pixelX, pixelY))
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }

            return false;
        }

        public bool GoToPosition(double longitude, double latitude)
        {
            if (mMap.GoToPosition(longitude, latitude))
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }

            return false;
        }

        public bool MoveUp()
        {
            if (mMap.MoveUp())
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }
            return false;
        }

        public bool MoveUp(int pixels)
        {
            if (mMap.MoveUp(pixels))
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }
            return false;
        }

        public bool MoveDown()
        {
            if (mMap.MoveDown())
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }
            return false;
        }

        public bool MoveDown(int pixels)
        {
            if (mMap.MoveDown(pixels))
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }
            return false;
        }

        public bool MoveLeft()
        {
            if (mMap.MoveLeft())
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }
            return false;
        }

        public bool MoveLeft(int pixels)
        {
            if (mMap.MoveLeft(pixels))
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }
            return false;
        }

        public bool MoveRight()
        {
            if (mMap.MoveRight())
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }
            return false;
        }

        public bool MoveRight(int pixels)
        {
            if (mMap.MoveLeft(pixels))
            {
                if (MapViewUpdated != null)
                {
                    MapViewUpdated();
                }

                return true;
            }
            return false;
        }

        public bool ZoomIn()
        {
            return mMap.ZoomIn();
        }

        public bool ZoomOut()
        {
            return mMap.ZoomOut();
        }
    }
}
